module.exports = function ($scope, $resource, toaster, date2str){

    $scope.searchform = {};

    $scope.start_time = {
        //'date': date2str(new Date()),
        'opened': false
    };

    $scope.end_time = {
        //'date': date2str(new Date()),
        'opened': false
    };

    $scope.open = function ($event, item) {
        $event.preventDefault();
        $event.stopPropagation();
        item.opened = true;
    };

    $scope.options1 = {
        legend: {
            display : true,
            position : 'right'
        }
    };
    
    $scope.load = function () {

        var s = '';
        var e = '';

        if($scope.start_time.date){
            if (typeof $scope.start_time.date === 'string') {
                s = $scope.start_time.date;
            } else {
                s = date2str($scope.start_time.date);
            }
        }
        if($scope.end_time.date){
            if (typeof $scope.end_time.date === 'string') {
                e = $scope.end_time.date;
            } else {
                e = date2str($scope.end_time.date);
            }
        }

        var para = {
            start_time : s == '' ? '' : s + " 00:00:00",
            end_time : e == '' ? '' : e + " 23:59:59",
        };
        angular.extend(para, $scope.searchform);

        var para1 = {'type':'14'};
        angular.extend(para1, para);
        $resource('/api/ac/goc/dataAnalysisService/singleData', {}, {}).save(para1, function (res) {
            console.log(res);

            if (res.errcode !== 0) {
                toaster.error({title: '', body: res.errmsg});
                return;
            }

            $scope.labels = res.data.labels;
            $scope.data = res.data.data;
            $scope.colors = [];
            for(var i in res.data.labels) {
                $scope.colors.push("#5bc0de");
            }

        });

        var para2 = {'type':'15'};
        angular.extend(para2, para);
        $resource('/api/ac/goc/dataAnalysisService/singleData', {}, {}).save(para2, function (res) {
            console.log(res);

            if (res.errcode !== 0) {
                toaster.error({title: '', body: res.errmsg});
                return;
            }

            $scope.labels1 = res.data.labels;
            $scope.data1 = res.data.data;
            $scope.colors1 = [];
            for(var i in res.data.labels) {
                $scope.colors1.push("#5bc0de");
            }

        });

        var para3 = {'type':'16'};
        angular.extend(para3, para);
        $resource('/api/ac/goc/dataAnalysisService/singleData', {}, {}).save(para3, function (res) {
            console.log(res);

            if (res.errcode !== 0) {
                toaster.error({title: '', body: res.errmsg});
                return;
            }

            $scope.labels2 = res.data.labels;
            $scope.data2 = res.data.data;
            $scope.colors2 = [];
            for(var i in res.data.labels) {
                $scope.colors2.push("#5bc0de");
            }

        });

        var para4 = {'type':'17'};
        angular.extend(para4, para);
        $resource('/api/ac/goc/dataAnalysisService/singleData', {}, {}).save(para4, function (res) {
            console.log(res);

            if (res.errcode !== 0) {
                toaster.error({title: '', body: res.errmsg});
                return;
            }

            $scope.labels3 = res.data.labels;
            $scope.data3 = res.data.data;

        });

    };
    $scope.load();

};